#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
from core import Core
from grid import TestPanel,Grid
from core.ide.datamangager import DataMangager
from core.ide.commandmanager import CommandManager
from core.utils.cmdenum import UPDATE_SETTINGS
from core.ide.themesmanager import ThemesManager, ThemeLib
from core.event.eventdispatcher import EventDispatcher
from core.event.event import DataEvent
from core.ide.langsmanager import LangsManager

class OptionPanel(wx.Frame):
    def __init__(self,title="Program Settings"):
        style=wx.DEFAULT_FRAME_STYLE | wx.SUNKEN_BORDER | wx.CLIP_CHILDREN
        self._frame=wx.Frame.__init__(self,Core().getMainFrame(), 1, title, size=(700,500),style=style)
        self.Bind(wx.EVT_CLOSE,self.onClose)
        
        
        self.panel=wx.Panel(self)
        self.panel.SetAutoLayout(True)
        
        
        
        self.right=RightPanel(self.panel)
        self.left=LeftPannel(self.panel)

        
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.AddSpacer(10)
        hbox.Add(self.left, 0, wx.EXPAND)
        hbox.AddSpacer(10)
        hbox.Add(self.right, 1, wx.EXPAND)
        hbox.AddSpacer(10)

        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.vbox.AddSpacer(10)
        self.vbox.Add(hbox, 1, wx.EXPAND)
        self.vbox.AddSpacer(10)
        self.panel.SetSizer(self.vbox)

        self.left.addEventListener(LeftPannel.SELECT,self.onSelect)
        
        
    def onClose(self,e=None):
        self.right.save()
        DataMangager().save()
        self.Unbind(wx.EVT_CLOSE,self)
        CommandManager().execute(UPDATE_SETTINGS)
        self.Destroy()
        if e:
            e.Skip()
    
    def onSelect(self,e=None):
        self.right.changeSection(e.data)
        
class LeftPannel(wx.Panel,EventDispatcher):
    
    SELECT ="Select"   
    
    def __init__(self,parent):
        wx.Panel.__init__(self,parent)
        EventDispatcher.__init__(self)
        box = wx.BoxSizer(wx.HORIZONTAL)
        self.SetSizer(box)
        #self.list=wx.ListBox(self, -1,size=(100,-1),choices=DataMangager().getSections())
        
        #self.list.SetSelection(0)
        self.list=wx.ListCtrl( self, -1,size=(120, 250),style=wx.LC_REPORT)
        
        info = wx.ListItem()
        info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
        info.m_image = -1
        info.m_format = 0
        info.m_text = "Sections"
        self.list.InsertColumnInfo(0, info)
        
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onItemSelected, self.list)
        self.populate()
        
        self.list.SetAutoLayout(True)
        box.Add(self.list, 1, wx.EXPAND)
        self.list.SetColumnWidth(0, self.list.GetSize()[0])
        
    def populate(self):
        import sys
        sections=DataMangager().getSections()
        self.list.SetImageList(ThemesManager().getDefaultTheme().getWxLib(ThemeLib.ICONE16), wx.IMAGE_LIST_SMALL)
        druid="Druid"
        scripts=[]
        plugins=[]
        
        
        for section in sections:
            if LangsManager().getLang(section.lower(),True):
                scripts.append(section)
            elif section!="Druid":
                plugins.append(section)

        img=ThemesManager().getDefaultTheme().getWxIdImage(ThemeLib.ICONE16,"druid")   
        self.list.InsertImageStringItem(sys.maxint, "Druid", img)
        
        for plugin in plugins:
            img=ThemesManager().getDefaultTheme().getWxIdImage(ThemeLib.ICONE16,"plugin")
            self.list.InsertImageStringItem(sys.maxint, plugin, img)
        
        for script in scripts:
            img=ThemesManager().getDefaultTheme().getWxIdImage(ThemeLib.ICONE16,"script")
            self.list.InsertImageStringItem(sys.maxint, script, img)
            #self.list.SetStringItem(section,0)
        
        
        
        
    def onItemSelected(self,e=None):
        self.dispatchEvent(DataEvent("Select",self.list.GetItemText(e.m_itemIndex)))
 

 
        
class RightPanel(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,parent)#,style=wx.BORDER_THEME
        self.grid=Grid(self)
       
        self.addHeader()
       
       
        
        self.box = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.box)
        #self.box.AddSpacer(5)
        self.box.Add(self.topBox, 0, wx.EXPAND)
        self.box.AddSpacer(5)
        self.box.Add(self.grid, 2, wx.EXPAND)
     
    def save(self):
        self.grid.save()
        
    def addHeader(self):
        #self.panel=wx.Panel(self,size=(-1,35))
        self.title=wx.StaticText(self)
        f=self.title.GetFont()
        f.SetWeight(wx.BOLD)
        self.title.SetFont(f)
        self.title.SetLabel("Druid")
        self.search = wx.SearchCtrl(self,pos=(0,5), size=(200,-1), style=wx.TE_PROCESS_ENTER)
        self.search.ShowSearchButton(1)
        self.search.ShowCancelButton(1)
        self.topBox = wx.BoxSizer(wx.HORIZONTAL)
        #self.panel.SetSizer(self.topBox)
        self.topBox.Add(self.title, 1, wx.EXPAND | wx.TOP,4)
        self.topBox.Add(self.search, 0, wx.ALIGN_RIGHT | wx.RIGHT)
        
    def changeSection(self,name):
        self.title.SetLabel(name)
        self.grid.setSection(name)